package com.qf.topic;

import com.qf.util.ConnUtil;
import com.rabbitmq.client.*;
import org.junit.Test;

import java.io.IOException;

public class Consumer {

    @Test
    public void orangeConsume() throws Exception {
        //1. Conn
        Connection conn = ConnUtil.getConn();
        //2. Channel
        Channel channel = conn.createChannel();
        //3. declare........


        channel.basicQos(1);
        //4. consume
        DefaultConsumer consume = new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("监听" + Publisher.ORANGE_QUEUE+ "的消费者,获取到消息:" + new String(body,"UTF-8"));
                channel.basicAck(envelope.getDeliveryTag(),false);
            }
        };
        channel.basicConsume(Publisher.ORANGE_QUEUE,false,consume);

        //5. close
        System.in.read();
        channel.close();
        conn.close();
    }
    @Test
    public void lazyRabbitConsume() throws Exception {
        //1. Conn
        Connection conn = ConnUtil.getConn();
        //2. Channel
        Channel channel = conn.createChannel();
        //3. declare........


        channel.basicQos(1);
        //4. consume
        DefaultConsumer consume = new DefaultConsumer(channel){
            @Override
            public void handleDelivery(String consumerTag, Envelope envelope, AMQP.BasicProperties properties, byte[] body) throws IOException {
                System.out.println("监听" + Publisher.LAZY_RABBIT_QUEUE + "的消费者,获取到消息:" + new String(body,"UTF-8"));
                channel.basicAck(envelope.getDeliveryTag(),false);
            }
        };
        channel.basicConsume(Publisher.LAZY_RABBIT_QUEUE,false,consume);

        //5. close
        System.in.read();
        channel.close();
        conn.close();
    }

}
